package pms.sg.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import pms.sg.service.ListSGService;
import pms.common.value.ProjectValue;
import pms.common.value.sgSearchValue;
import pms.common.value.sgUserValue;
import pms.common.value.sgListValue;


public class ListSGAction extends ActionSupport{

	private static final long serialVersionUID = 1L;
	private sgUserValue user;
	public sgUserValue getUser() {
		return user;
	}

	public void setUser(sgUserValue user) {
		this.user = user;
	}

	private ListSGService listSGService;
	private sgListValue zhlist;
	private Collection<sgListValue> zhlists;
	private String all_select;
	private String quickSearchbuttonID;
	public String getQuickSearchbuttonID() {
		return quickSearchbuttonID;
	}

	public void setQuickSearchbuttonID(String quickSearchbuttonID) {
		this.quickSearchbuttonID = quickSearchbuttonID;
	}

	private sgSearchValue search = new sgSearchValue();
	private String[] banGos;
	private int pageNow = 1 ; 
	private int pagePre = 0; 
	private int pageNext = 2; 
    private int pageSize = 10 ;
    private int totalPages;
    private int listsize=0;
	
	public String[] getBanGos() {
		return banGos;
	}

	public void setBanGos(String[] banGos) {
		this.banGos = banGos;
	}

	@SuppressWarnings("unchecked")
	public List getMonthArray() {
		List l_list = new ArrayList();
		for (int i=0;i<12;i++) {
			if (i<9) {
				l_list.add(i,"0" + (i+1));
			} else {
				l_list.add(i,"" + (i+1));
			}
		}
		return l_list;
	}
	
	@SuppressWarnings("unchecked")
	public List getDayArray() {
		List l_list = new ArrayList();
		for (int i=0;i<31;i++) {
			if (i<9) {
				l_list.add(i,"0" + (i+1));
			} else {
				l_list.add(i,"" + (i+1));
			}
		}
		return l_list;
	}
	

	//プロジェクト
	public List<ProjectValue> getProjectList() {
		return listSGService.getProjectList();
	}

	// 所属テーマ
	@SuppressWarnings("unchecked")
	public List<HashMap> getFunctionArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return listSGService.getFunctionArray();
    }

	// テスト段階
	@SuppressWarnings("unchecked")
	public List<HashMap> getTestlvlArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return listSGService.getTestlvlArray();
    }

	// 検出側
	@SuppressWarnings("unchecked")
	public List<HashMap> getTestPartArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return listSGService.getTestPartArray();
    }
	
	// 検出者
	@SuppressWarnings("unchecked")
	public List<HashMap> getTesterArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return listSGService.getUserArray();
    }
	
	// 修正担当
	@SuppressWarnings("unchecked")
	public List<HashMap> getModifyArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return listSGService.getUserArray();
    }
	
	// ステータス
	@SuppressWarnings("unchecked")
	public List<HashMap> getStatusArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        return listSGService.getStatusArray();
    }
	
	// 障害要因
	@SuppressWarnings("unchecked")
	public List<HashMap> getReasonArray() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {   
        return listSGService.getReasonArray();
    }
	
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {

		ArrayList zhliststmp = new ArrayList();
		System.out.println("search:" + search);
		zhliststmp = listSGService.getSGLists(search);
    	Map session = ActionContext.getContext().getSession();
    	session.put("zhlists",zhliststmp);
    	setAll_select("");
    	pageCut();
		return SUCCESS;
	}
	
	//全件検索処理
	@SuppressWarnings("unchecked")
	public String allDataSearch() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		ArrayList zhliststmp = new ArrayList();
		search = new sgSearchValue();
		zhliststmp = listSGService.getSGLists(search);
    	Map session = ActionContext.getContext().getSession();
    	session.put("zhlists",zhliststmp);
    	setAll_select("");
    	pageCut();
		return SUCCESS;
	}
	
	//速い条件検索処理
	@SuppressWarnings("unchecked")
	public String quickSearch() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
		System.out.println("quickSearchbuttonID --: "+quickSearchbuttonID);

		if("btn_1".equals(quickSearchbuttonID)){
			search.setProjectId("1");
		}
		else if("btn_2".equals(quickSearchbuttonID)){
			search.setProjectId("2");
		}
		else if("btn_3".equals(quickSearchbuttonID)){
			search.setProjectId("保険金SP");
		}
		else if("btn_4".equals(quickSearchbuttonID)){
			search.setProjectId("ＶＢ現行廃止");
		}
		else{
			return allDataSearch();
		}
		return execute();
	}
	
	@SuppressWarnings("unchecked")
	public String pageCut() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		zhlists = new ArrayList<sgListValue>();
		banGosList();
		
		Map session = ActionContext.getContext().getSession();
		ArrayList alzhlists = (ArrayList)session.get("zhlists");
		
		setListsize(alzhlists.size());
		setTotalPages(Integer.parseInt(String.valueOf(Math.ceil((listsize+0.0)/(pageSize+0.0))).substring(0,1)));
		
    	int min = (pageNow - 1) * pageSize;
    	int max = pageNow * pageSize;
    	if(listsize<min){
    		min = listsize;
    	}
    	if(listsize<max){
    		max = listsize;
    	}
    	System.out.println("min : "+min+"----max : "+max);
    	System.out.println("TotalPages : "+totalPages);
    	List currentPageList = alzhlists.subList(min,max);
    	for (int i=0;i<currentPageList.size();i++){
    		zhlists.add((sgListValue)currentPageList.get(i));
    	}
    	return SUCCESS;
	}
	@SuppressWarnings("unchecked")
	public void banGosList(){
		if (all_select == null || all_select.equals("")) {
			all_select = "";
		}
		if(all_select != ""){
			String banGostr = all_select;
			banGostr = banGostr.replace("[","").replace("]","");
			setBanGos(banGostr.split(","));
		}
		System.out.println("all_select : "+all_select);
	}
	//削除処理
	public String deleteData() {
		banGosList();
		if(banGos.length<1){return SUCCESS;}
		boolean l_flg = listSGService.deleteData(banGos);
		if (l_flg) {
			return SUCCESS;
		} else {
			return ERROR;
		}
	}
	
	//export
	public String getDownloadFileName() throws UnsupportedEncodingException {
		return listSGService.getDownloadFileName(); 
	}
	public InputStream getInputStream() throws Exception {
		banGosList();
		return listSGService.expExcelData(banGos);
	}
	public String export() throws Exception {
		return SUCCESS;
	}
	public sgListValue getZhlist() {
		return zhlist;
	}
	public void setZhlist(sgListValue zhlist) {
		this.zhlist = zhlist;
	}
	public Collection<sgListValue> getZhlists() {
		return zhlists;
	}
	public void setZhlists(Collection<sgListValue> zhlists) {
		this.zhlists = zhlists;
	}
	public ListSGService getListSGService() {
		return listSGService;
	}
	public void setListSGService(ListSGService listSGService) {
		this.listSGService = listSGService;
	}
	public sgSearchValue getSearch() {
		return search;
	}
	public void setSearch(sgSearchValue search) {
		this.search = search;
	}

	public String getAll_select() {
		return all_select;
	}

	public void setAll_select(String all_select) {
		this.all_select = all_select;
	}

	public int getPageNext() {
		return pageNext;
	}

	public void setPageNext(int pageNext) {
		this.pageNext = pageNext;
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		setPagePre(pageNow-1);
		setPageNext(pageNow+1);
		System.out.println("PageNext"+pageNext);
		System.out.println("PagePre"+pagePre);
		this.pageNow = pageNow;
	}

	public int getPagePre() {
		return pagePre;
	}

	public void setPagePre(int pagePre) {
		this.pagePre = pagePre;
	}

	public int getListsize() {
		return listsize;
	}

	public void setListsize(int listsize) {
		this.listsize = listsize;
	}

	public int getTotalPages() {
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}
	
	
}
